package com.winshare.saleAssistant.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winshare.saleAssistant.dao.DataStaticMapper;
import com.winshare.saleAssistant.dao.SchoolHardwareMapper;
import com.winshare.saleAssistant.dao.TUserMapper;
import com.winshare.saleAssistant.domain.dto.req.DataAnalysisQueryDTO;
import com.winshare.saleAssistant.model.ProductStatistics;
import com.winshare.saleAssistant.model.RegionStatistics;
import com.winshare.saleAssistant.model.ReportStatistics;
import com.winshare.saleAssistant.model.SchoolRankingModel;
import com.winshare.saleAssistant.service.IDataStaticService;
import com.winshare.saleAssistant.util.DateUtil;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DataStaticServiceImpl extends ServiceImpl<DataStaticMapper, ProductStatistics> implements IDataStaticService {

    @Resource
    private DataStaticMapper dataStaticMapper;

    @Resource
    private SchoolHardwareMapper schoolHardwareMapper;

    @Resource
    private TUserMapper userMapper;

    @Override
    public List<ProductStatistics> getProductReport(String time) {
        return dataStaticMapper.getProductReportFormWarpper(time);
    }

    @Override
    public List<RegionStatistics> getCityReport(String time, Integer pageNum, Integer pageSize) {
        return dataStaticMapper.getCityReportFormWarpper(time, pageNum, pageSize);
    }

    @Override
    public List<RegionStatistics> getAreaReport(String time, Integer pageNum, Integer pageSize) {
        return dataStaticMapper.getAreaReportFormWarpper(time, pageNum, pageSize);
    }

    @Override
    public ReportStatistics getReportForm(String time) {
        ReportStatistics reportStatistics = dataStaticMapper.getReportFormWarpper(time, time.split("-")[0], DateUtil.getHbDate(time).substring(0, 7), DateUtil.getTbDate(time).substring(0, 7));
        reportStatistics.setProductReport(dataStaticMapper.getProductReportFormWarpper(time));
        reportStatistics.setAreaReport(dataStaticMapper.getAreaReportFormWarpper(time, 0, 3));
        reportStatistics.setCityReport(dataStaticMapper.getCityReportFormWarpper(time, 0, 3));
        return reportStatistics;
    }

    @Override
    public List<Map<String, Object>> getModularUseTime(Date starTime, Date endTime, String productName, String areaName, String orderBy, String productCode) {
        return dataStaticMapper.getModularUseTime(starTime, endTime, productName, areaName, orderBy, productCode);
    }

    @Override
    public List<Map<String, Object>> getFunUseNum(Date starTime, Date endTime, String productName, String areaName, String orderBy) {
        return dataStaticMapper.getFunUseNum(starTime, endTime, productName, areaName, orderBy);
    }

    @Override
    public Map<String, Object> getTotal(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getTotal(starTime, endTime, productName, areaName);
    }

    @Override
    public Map<String, Object> getTotal1(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getTotal1(starTime, endTime, productName, areaName);
    }

    @Override
    public Map<String, Object> getTotal2(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getTotal2(starTime, endTime, productName, areaName);
    }

    @Override
    public Map<String, Object> getTotal3(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getTotal3(starTime, endTime, productName, areaName);
    }

    @Override
    public Map<String, Object> getTotal4(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getTotal4(starTime, endTime, productName, areaName);
    }

    @Override
    public Map<String, Object> getUserSexTotal(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getUserSexTotal(starTime, endTime, productName, areaName);
    }

    @Override
    public List<Map<String, Object>> getUserAge(Date starTime, Date endTime, String productName, String areaName) {
        return dataStaticMapper.getUserAge(starTime, endTime, productName, areaName);
    }

    /**
     * 获取城市排名
     *
     * @param dataAnalysisQueryDTO
     * @return
     */
    @Override
    public List<Map<String, Object>> getCityRanking(DataAnalysisQueryDTO dataAnalysisQueryDTO) {
        return this.baseMapper.getCityRanking(dataAnalysisQueryDTO);
    }

    /**
     * 获取区县排名
     *
     * @param dataAnalysisQueryDTO
     * @return
     */
    @Override
    public List<Map<String, Object>> getAreaRanking(DataAnalysisQueryDTO dataAnalysisQueryDTO) {
        return this.baseMapper.getAreaRanking(dataAnalysisQueryDTO);
    }

    /**
     * 获取区域排名
     *
     * @param dataAnalysisQueryDTO
     * @return
     */
    @Override
    public List<Map<String, Object>> getParentAreaRanking(DataAnalysisQueryDTO dataAnalysisQueryDTO) {
        return this.baseMapper.getParentAreaRanking(dataAnalysisQueryDTO);
    }

    /**
     * 获取学校排名
     *
     * @param dataAnalysisQueryDTO
     * @return
     */
    @Override
    public Page<Map<String, Object>> getSchoolRanking(DataAnalysisQueryDTO dataAnalysisQueryDTO) {
        int totalCount = baseMapper.getSchoolRankingCount(dataAnalysisQueryDTO);
        Integer pageIndex = dataAnalysisQueryDTO.getPageIndex();
        Integer pageSize = dataAnalysisQueryDTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        } else {
            return new Page<>(pageIndex, pageSize, totalCount, this.baseMapper.getSchoolRanking(dataAnalysisQueryDTO));
        }
    }

    @Override
    public List<SchoolRankingModel> getSchoolRankingList(DataAnalysisQueryDTO dataAnalysisQueryDTO) {
        return  this.baseMapper.getSchoolRankingList(dataAnalysisQueryDTO);
    }

    /**
     * 获取用户排名
     *
     * @param dataAnalysisQueryDTO
     * @return
     */
    @Override
    public Page<Map<String, Object>> getUserRanking(DataAnalysisQueryDTO dataAnalysisQueryDTO) {
        int totalCount = this.baseMapper.getUserRankingCount(dataAnalysisQueryDTO);
        Integer pageIndex = dataAnalysisQueryDTO.getPageIndex();
        Integer pageSize = dataAnalysisQueryDTO.getPageSize();
        if (0 == totalCount) {
            return new Page<>(pageIndex, pageSize, totalCount, new ArrayList<>());
        } else {
            return new Page<>(pageIndex, pageSize, totalCount, this.baseMapper.getUserRanking(dataAnalysisQueryDTO));
        }
    }
}
